using System;
using UnityEngine;
using QFramework;
using Unity.VisualScripting;
using Random = UnityEngine.Random;

namespace QFramework.Monk
{
	public partial class CameraController : ViewController
	{
		private Vector2 mTargetPos;
		
		public static CameraController Default;
		private int mShakeFrame = 0;
		private float mShkeR = 0;
		private bool  isShake = false;
		
		public void Shake(int frameCount,float shkeR)
		{
			mShakeFrame = frameCount;
			mShkeR =shkeR;
			isShake = true;
		}
		void Awake()
		{
			Default = this;
			
		}

		private void OnDestroy()
		{
			Default = null;
		}

		void Start()
		{
			// Code Here
		}

		void LateUpdate()
		{
			if (Player.Default)
			{

				if (isShake)
				{
						mShakeFrame --;
						var shakeR = Mathf.Lerp(mShkeR, 0, mShakeFrame / 30f);
						transform.PositionX(transform.position.x + Random.Range(-shakeR, shakeR));
						transform.PositionY(transform.position.y + Random.Range(-shakeR, shakeR));
						if (mShakeFrame<=0)
						{
							isShake = false;
						}
				}
				else
				{
					mTargetPos = Player.Default.transform.position;
					//相机跟随玩家
					transform.PositionX((1f - Mathf.Exp(-5f * Time.fixedDeltaTime)).Lerp(transform.position.x, mTargetPos.x));
					transform.PositionY((1f - Mathf.Exp(-5f * Time.fixedDeltaTime)).Lerp(transform.position.y, mTargetPos.y));
					
				}
				
			}
		}
	}
}
